package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.dao.IMediaerInfoDao;
import com.magicode.cms.entiy.base.Enterprise;
import com.magicode.cms.entiy.base.MediaerInfo;
import com.magicode.cms.service.IMediaerInfoService;
import com.magicode.common.dao.ICommonDaoService;

@Service("servicemediaerInfo")
public class MediaerInfoServiceImpl extends BaseServiceImpl<MediaerInfo, IMediaerInfoDao> implements IMediaerInfoService {

	@Autowired
	@Qualifier("daomediaerInfo")
	private IMediaerInfoDao mediaerInfoDao;
	
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "名称");
		map.put("width", 220);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "email");
		map.put(json_title, "邮箱");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "tel");
		map.put(json_title, "电话");
		map.put("width", 120);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "qq");
		map.put(json_title, "QQ");
		map.put("width", 120);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql ="from " +MediaerInfo.class.getName()+" "+entity_alias_name+" ";
		
		if(null!=querywhere && !querywhere.isEmpty()){
			hql += "where 1 = 1 ";
			if(querywhere.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+querywhere.get(field_id)+"'";
			}
			if(querywhere.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".title like '%"+querywhere.get("title")+"%'";
			}
		}
		return hql;
	}

	@Override
	public MediaerInfo get(String id) {
		String hql = " from com.magicode.cms.entiy.base.MediaerInfo m  where m.id=? ";
		MediaerInfo mediaerInfo = null;
		try {
			mediaerInfo = (MediaerInfo) this.commonDaoService.getByHQL(hql, new String[]{id});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mediaerInfo;
	}
	
	@Override
	public MediaerInfo getMediaerInfo() {
		String sql = "select m " +
				" from "+MediaerInfo.class.getName()+" m";
		List<MediaerInfo> list = new ArrayList<MediaerInfo>();
		try {
			list = this.commonDaoService.queryList(sql, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MediaerInfo mediaerInfo = null;
		if(list.size()>0 && null!=list){
			mediaerInfo = list.get(0);
		}
		return mediaerInfo;
	}
	
	@Override
	public List<MediaerInfo> listMedaierInfos() {
		String sql = "select m.id,m.title,m.yellowPageInfoText,m.email,m.tel,m.qq from mediaer_info m";
		List<MediaerInfo> list = new ArrayList<MediaerInfo>();
		try {
			list = this.commonDaoService.queryList(sql, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("title", ""+entity_alias_name+".title as title ");
			gridsel.put("email", ""+entity_alias_name+".email as email ");
			gridsel.put("tel", ""+entity_alias_name+".tel as tel ");
			gridsel.put("qq", ""+entity_alias_name+".qq as qq ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public Long getCount() {
		String hql = " select count(m.id) from "+ MediaerInfo.class.getName()+" m  " ;
//		Long count = this.commonDaoService.countByHql(hql, null);
		Long count = mediaerInfoDao.queryCount(hql);
		return count;
	}
	
	@Override
	public IMediaerInfoDao getDao() throws Exception {
		return mediaerInfoDao;
	}

	public IMediaerInfoDao getMediaerInfoDao() {
		return mediaerInfoDao;
	}

	public void setMediaerInfoDao(IMediaerInfoDao mediaerInfoDao) {
		this.mediaerInfoDao = mediaerInfoDao;
	}

}
